﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
namespace BallData
{
    
    class WebUtil
    {
        public byte[] m_postdata;
       
        public WebData m_onwebdata;
        public Object m_contextobj;
        public System.Object Contextobj
        {
            get { return m_contextobj; }
            set { m_contextobj = value; }
        }
        private String m_url;
        public System.String Url
        {
            get { return m_url; }
            set { m_url = value; }
        }
        public BallData.WebData Onwebdata
        {
            get { return m_onwebdata; }
            set { m_onwebdata = value; }
        }
        public byte[] Postdata
        {
            get { return m_postdata; }
            set { m_postdata = value; }
        }
        public static void PostData(String posturl,byte[] data)
        {
            WebUtil web = new WebUtil();
            web.Postdata = data;
            web.Url = posturl;
            web.WebPostData();
        }
        public static void GetData(String geturl,WebData webdata,Object context)
        {
            Console.WriteLine(geturl);
            WebUtil web = new WebUtil();
            web.Onwebdata = webdata;
            web.Url = geturl;
            web.Contextobj = context;
            web.WebGetData();
        }
      
        public void WebPostData()
        {
            WebClient webclient = new WebClient();
            webclient.UploadDataAsync(new Uri(this.Url),Postdata);
            webclient.UploadDataCompleted += new UploadDataCompletedEventHandler(webclient_UploadDataCompleted);

        }

        void webclient_UploadDataCompleted(object sender, UploadDataCompletedEventArgs e)
        {
            WebClient webclient = sender as WebClient;

            Console.WriteLine("webclient_UploadDataCompleted");
        }
        public void OnGetRequestStreamOk(IAsyncResult asyncResult)
        {
            try
            {
                HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
               
                Stream requestStream = request.EndGetRequestStream(asyncResult);
                StreamWriter writestream = new StreamWriter(requestStream);
                writestream.Write(Postdata);
                writestream.Flush();
                writestream.Close();
                Console.WriteLine("post data ok!");
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            
        }

        public void WebGetData()
        {
            HttpWebRequest webrequest = HttpWebRequest.Create(this.Url) as HttpWebRequest;
            
            webrequest.Method = "GET";
           
            
            try
            {
                IAsyncResult asyncResult = webrequest.BeginGetResponse(new AsyncCallback(OnGetResponseOk), webrequest);
            }
            catch(System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
        public void OnGetResponseOk(IAsyncResult asyncResult)
        {
            try
            {
                HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;

                WebResponse response = request.EndGetResponse(asyncResult);

                Stream stream = response.GetResponseStream();

                MemoryStream buf = new MemoryStream();
                byte[] data = new byte[4096];
                int len = stream.Read(data, 0, 4096);
                while ((len != -1 && len != 0))
                {
                    buf.Write(data, 0, len);
                    if (stream.CanRead)
                        len = stream.Read(data, 0, 4096);
                    else
                        break;
                }
                
                if (this.Onwebdata != null)
                {
                    this.Onwebdata.OnWebData(buf.ToArray(),this.Url,this.Contextobj);
                }
                
            }
            catch (System.Exception ex)
            {
                if (this.Onwebdata != null)
                {
                    this.Onwebdata.OnWebDataError(this.Url);
                }
                Console.WriteLine(ex.Message);
            }
        }

        public static String getDataByUrl(String url,Encoding encode)
        {


            HttpWebRequest httprequest = (HttpWebRequest)WebRequest.Create(url);

            httprequest.ContentType = "text/html; charset=utf-8";

            HttpWebResponse response = null;
            try
            {
                response = (HttpWebResponse)httprequest.GetResponse();
            }
            catch (System.Exception ex)
            {
                Console.Write(ex.Message);
                return "";
            }



            StreamReader sr = new StreamReader(response.GetResponseStream(), encode);
            // 得到赛事数据
            String szmatchdata = sr.ReadToEnd();
            sr.Close();
            response.Close();

            return szmatchdata;

        }
    }
}
